#include "inc_test.h"

// #define MIN_STACKSIZE   (4096) /* 注意redzone,*/
#define TEST_NR		 (1000000)

CROUT_STRUCT *crs[1];
static void *rout(void *arg){
	(void)arg;
	word_t volatile time;
	for (word_t i = 0; i < TEST_NR; i++){
		CROUT_YIELD(crs[0]);
		time = i;
	}
	return (void *)time;
}

void test_ctx(){
	CROUT_ATTR test_attr;
	test_attr.share_stack = NULL;
	struct timespec ts;
	struct timespec dt;
	word_t cycles0;
	word_t cycles1;
	word_t time = TEST_NR;

	test_attr.stack_size = MIN_STACKSIZE;
	if (0 != CROUT_CREATE(&crs[0], &test_attr, rout, (void *)time)){
		printf("create failed\n");
		return;
	}
	TIME_STAMP(&ts);
	cycles0 = rdcnt_relax();
	for(unsigned long idx = 0; idx < time; idx++){
		CROUT_RESUME(crs[0]);
	}
	cycles1 = rdcnt_relax();
	TIME_STAMP(&dt);
	// printf("[ ns] %lu switch time [us] %lu[%ld]\n", (unsigned long)time, TIME_DELTA_US(&ts, &dt), cycles1-cycles0);
	printf("[%8llu ns/ops] %lu switch time in us: %llu (%lld cycles/op)\n", TIME_DELTA_NS(&ts, &dt)/time, \
			(unsigned long)time, TIME_DELTA_US(&ts, &dt), ((llong_t)cycles1-cycles0)/TEST_NR);
	fprintf(stderr, "%llu\n", TIME_DELTA_US(&ts, &dt));
	for(unsigned long idx = 0; idx < 1; idx++){
		CROUT_RELEASE(crs[idx]);
	}
}

int main(){
	test_ctx();
	return 0;
}
